package com.trxy.controller.front;

import com.trxy.model.Customer;
import com.trxy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/front")
public class LoginController {
    @Autowired
    CustomerService customerService;

    @RequestMapping("/login")
    String login() {
        return "front/login/view";
    }

    @RequestMapping("/dologin")
    String dologin(HttpServletRequest request, String name, String password) {
        //进行输出验证
        System.out.println(name + password);

        Customer customer = customerService.selectByNameAndPassword(name, password);
        if (customer == null) {
            return "redirect:/front/login";//重定向回  登录页
        } else {
            //加入到session
            request.getSession().setAttribute("CUSTOMER", customer);
            return "redirect:/front/browse/product/list";//重定向到  商品列表页
        }
    }
    //register
    @RequestMapping("/zhuCe")
    String dengLu(){
        return "front/login/register";
    }
    @RequestMapping("/register")
    String register(String name, String password, String password1, String nick, Model model) {
        if(password.equals (password1)){
            customerService.addCustomer(name,password,nick);
            return "redirect:/front/login";  //重定向回到商品列表页
        }else{
            model.addAttribute ("pass","密码不一致");
            return "front/login/register";
        }

    }

    @RequestMapping("/logout")
    String logout(HttpSession session) {
        session.removeAttribute("CUSTOMER"); //把session中的顾客对象清除
        return "redirect:/front/browse/product/list";  //重定向回到商品列表页
    }
}
